# -*- coding:utf-8 -*-
# -------------------------------------------------------------------------------
# Description:  
# Reference:
# Author: 安智军
# Date:   2021/11/17
# -------------------------------------------------------------------------------
'''
封装日志器
'''

import logging
import logging.handlers
from config import basepath


class GetLog:
    __logger = None  # 新建日志器变量

    @classmethod
    def get_logger(cls):
        if cls.__logger is None:  # 判断日志器是否为空
            cls.__logger = logging.getLogger()  # 如果为空获取日志器
            cls.__logger.setLevel(logging.INFO)  # 修改日志器的默认级别
            log_path = basepath + "/Outputs/Logs/WMS_v1_UI.log"  # 指定日志生成的路径
            # print(log_path) # 日志生成的路径 D:\接口自动化v2.0\WMS_UI_V1_Demo/log/WMS_v1_UI.log

            # 获取处理器
            th = logging.handlers.TimedRotatingFileHandler(filename=log_path, when="midnight", interval=1,
                                                           backupCount=3, encoding="utf-8")
            # 获取格式器
            fmt = '%(asctime)s %(levelname)s [%(name)s] [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s'  # 时间, 日志级别, Logger的名称,文件名, 函数/方法名, 行数, 输出信息
            fm = logging.Formatter(fmt)
            # 将格式器天机到处理器pu中
            th.setFormatter(fm)
            # 将处理器添加到日志中
            cls.__logger.addHandler(th)

        # 返回日志器
        return cls.__logger


if __name__ == '__main__':
    log = GetLog.get_logger()  # 调用日志器
    log.info("测试信息级别日志")  # 2021-11-17 10:24:51,475 INFO [root] [GetLog.py(<module>:45)] - 测试信息级别的日志
    log.error("测试错误级别") # 2021-11-17 10:26:33,749 ERROR [root] [GetLog.py(<module>:46)] - 测试错误级别
